const { app, Menu } = require('electron')
const { showAboutDialog, toggleTransparency, toggleFloatingBall } = require('./window')

// 创建菜单模板
function getMenuTemplate() {
  return [
    {
      label: '文件(&F)',
      submenu: [
        {
          label: '新建(&N)',
          accelerator: 'CmdOrCtrl+N',
          click: () => {
            console.log('新建文件')
          }
        },
        {
          label: '打开(&O)',
          accelerator: 'CmdOrCtrl+O',
          click: () => {
            console.log('打开文件')
          }
        },
        { type: 'separator' },
        {
          label: '退出(&Q)',
          accelerator: 'CmdOrCtrl+Q',
          click: () => {
            app.quit()
          }
        }
      ]
    },
    {
      label: '编辑(&E)',
      submenu: [
        { label: '撤销(&U)', role: 'undo' },
        { label: '重做(&R)', role: 'redo' },
        { type: 'separator' },
        { label: '剪切(&T)', role: 'cut' },
        { label: '复制(&C)', role: 'copy' },
        { label: '粘贴(&P)', role: 'paste' },
        { label: '删除(&D)', role: 'delete' },
        { type: 'separator' },
        { label: '全选(&A)', role: 'selectAll' }
      ]
    },
    {
      label: '视图(&V)',
      submenu: [
        { label: '重新加载(&R)', role: 'reload' },
        { type: 'separator' },
        { label: '重置缩放(&O)', role: 'resetZoom' },
        { label: '放大(&I)', role: 'zoomIn' },
        { label: '缩小(&O)', role: 'zoomOut' },
        { type: 'separator' },
        { label: '全屏(&F)', role: 'togglefullscreen' },
        {
          label: '悬浮球(&L)',
          click: () => {
            toggleFloatingBall()
          }
        },
        { type: 'separator' },
        {
          label: '透明(&T)',
          click: () => {
            toggleTransparency()
          }
        },
        {
          label: '打开开发者工具(&O)',
          accelerator: 'CmdOrCtrl+Shift+I',
          click: (_, window) => {
            if (window) window.webContents.openDevTools()
          }
        }
      ]
    },
    {
      label: '帮助(&H)',
      submenu: [
        {
          label: '关于(&A)',
          click: () => {
            showAboutDialog()
          }
        }
      ]
    }
  ]
}

// 设置应用菜单
function setupMenu() {
  const menu = Menu.buildFromTemplate(getMenuTemplate())
  Menu.setApplicationMenu(menu)
}

module.exports = {
  getMenuTemplate,
  setupMenu
}